<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 14:41
 */
namespace hook;


use app\admin\model\Member;
use app\admin\model\ScoreLog;
use think\Loader;
use think\Request;
use think\Session;

/**
 * 用户积分管理
 * Class AccessAuth
 * @package hook
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/05/12 11:59
 */
class Score
{

    /**
     * 当前请求对象
     * @var Request
     */
    protected $request;

    /**
     * 行为入口
     * @param $params
     */
    public function run(){}


    /**
     * 添加积分
     * @param $params
     */
    public static function addScore(&$params){
        if(Member::where('id',$params['id'])->setInc('integral',$params['integral'])){
            !isset($params['type']) && $params['type'] = 1;
            self::scoreLog($params);
        }
    }

    /**
     * 减积分
     * @param $params
     */
    public static function reduceScore(&$params){
        if(Member::where('id',$params['id'])->setDec('integral',$params['integral'])){
            !isset($params['type']) && $params['type'] = 2;
            self::scoreLog($data);
        }
    }

    /**
     * 添加积分明细
     * @param $params
     */
    public static function scoreLog(&$params){
        $data = [
            'uid' => $params['id'],
            'score' => $params['integral'],
            'from' => $params['from'],
        ];
        if(isset($params['type']) && $params['type'] == 2){
            $data['type'] = $params['type'];
        }else{
            $data['operator'] = Session::get('user.username');
        }
        $scoerLogModel = Loader::model('admin/ScoreLog');
        $scoerLogModel->allowField(true)->save($data);
    }

}
